<template>
	<view class="content">
		<view class="logo"><image src="../../static/mylogo.jpg" mode=""></image></view>
		<view class="uni-form-item uni-column">
			<input type="tel" class="uni-input" v-model="username" placeholder="请输入手机号" />
		</view>
		<!-- <view class="uni-form-item uni-column column-with-btn">
			<input type="text" class="uni-input" name="" placeholder="请输入图片验证码" v-model="captchaImg" />
			<view class="send-code-btn captcha-view" @click="captcha('refreshCaptcha')">
				<i v-if="captchaing" class="uni-icon_toast uni-loading"></i>
				<img v-if="!captchaing" :src="captchaBase64" width="100%" height="100%"></img>
			</view>
		</view>
		<view class="uni-form-item uni-column column-with-btn">
			<input type="number" class="uni-input" name="" placeholder="请输入验证码" />
			<button :class="{active : !disableCodeBtn}" :disabled="disableCodeBtn" @tap="sendCode">{{codeBtn.text}}</button>
		</view> -->
		<view class="uni-form-item uni-column">
			<input type="password" class="uni-input" v-model="password" placeholder="请输入密码" />
		</view>
		<view class="uni-form-item uni-column">
			<input type="tel" class="uni-input" v-model="invitation" placeholder="请输入邀请码" />
		</view>
		<button @click="register()" type="primary">注册</button>
		<view class="links"><view class="link-highlight" @tap="gotoLogin">点此登陆</view><view @tap="gotoForgetPassword">忘记密码？</view></view>
	</view>
</template>

<script>
	const captchaOptions = {
		deviceId: getDeviceUUID(),
		scene: 'login'
	}
	import {
		getDeviceUUID
	} from '@/common/utils.js'
	export default {
		data() {
			return {
				captchaImg: '',
				username:'',
				invitation:'',
				password:'',
				seconds: 10,
				captchaing: false,
				captchaBase64: '',
				codeBtn: {
					text: '获取验证码',
					waitingCode: false,
					count: this.seconds
				}
			}
		},
		onLoad(option) {
			console.log(option)
			if(option.invitation){
				this.invitation=option.invitation
			}
			this.captcha('createCaptcha')
		},
		methods: {
			gotoForgetPassword: function () {
				uni.navigateTo({url: 'forget-password'});
			},
			async register(){
				let {
					result: res
				} = await uniCloud.callFunction({
					name: 'user-center',
					data: {
						action:'register',
						params: {username:this.username,password:this.password}
					}
				})
				if(res.code==0){
					uni.showModal({
						content:'注册成功请登陆',
						success:(re)=>{
							if (re.confirm) {
							    uni.navigateTo({
							    	url:'login'
							    })
							}
						}
					})
				}else{
					uni.showModal({
						content:res.message
					})
				}
			},
			async captcha(action, args) {
				if (this.captchaing) return;
			
				// 验证不loading
				this.captchaing = true;
				console.log(captchaOptions,args)
				let {
					result: res
				} = await uniCloud.callFunction({
					name: 'user-center',
					data: {
						action,
						params: captchaOptions
					}
				})
				this.captchaing = false;
				if (res.code === 0) {
					this.captchaBase64 = res.captchaBase64
				} else {
					uni.showToast({
						icon: 'none',
						title: res.msg,
						duration: 1000
					})
				}
				return res;
			},
			sendCode: function () {
				this.codeBtn.waitingCode = true;
				this.codeBtn.count = this.seconds;
				this.codeBtn.text = this.codeBtn.count + 's';
				
				let countdown = setInterval( () => {
					this.codeBtn.count--;
					this.codeBtn.text = this.codeBtn.count + 's';
					if( this.codeBtn.count < 0 ){
						clearInterval(countdown);
						this.codeBtn.text = '重新发送';
						this.codeBtn.waitingCode = false;
					}
				},1000);
			},
			gotoLogin: function () {
				uni.navigateTo({
					url: 'login'
				})
			}
		},
		computed: {
			disableCodeBtn: function (){
				return this.codeBtn.waitingCode || this.captchaImg.length < 4;
			} 
		}
	}
</script>

<style lang="scss" scoped>
	$color-primary: #b49950;
	.content{
		padding: 60upx 100upx 100upx;
	}
	.logo{
	    text-align: center;
		image{
		    height: 200upx;
		    width: 200upx;
		    margin: 0 0 40upx;
		}
	}
	.uni-form-item{
		margin-bottom: 40upx;
		padding: 0;
		border-bottom: 1px solid #e3e3e3;
		.uni-input{
			font-size: 30upx;
			padding: 7px 0;
		}
	}
	.column-with-btn{
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		button{
			font-size: 24upx;
			margin: 0;
			width: 180upx;
			text-align: center;
			&:after{
				border: none
			}
			&.active{
				background-color: $color-primary;
				color: $uni-text-color-inverse;
			}
		}
	}
	.img-captcha{
		width: 150upx;
		height: 60upx;
	}
	button[type="primary"]{
		background-color: $color-primary;
		border-radius: 0;
		font-size: 34upx;
		margin-top: 60upx;
	}
	.links{
		text-align: center;
		margin-top: 40upx;
		font-size: 26upx;
		color: #999;
		view{
			display: inline-block;
			vertical-align: top;
			margin: 0 10upx;
		}
		.link-highlight{
			color: $color-primary
		}
	}
</style>
